package ru.arriah.servicedesk.web.leadExecutor.action;

import java.io.IOException;
import java.util.Calendar;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.EmployeeBean;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.bean.RequestPriorityBean;
import ru.arriah.servicedesk.bean.TaskBean;
import ru.arriah.servicedesk.ejb.EmployeeManagerLocalHome;
import ru.arriah.servicedesk.ejb.EmployeeManagerLocalObject;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;


public class DisplayTaskFormAction  extends DumbAction {
  protected RequestManagerLocalHome requestManagerLocalHome;
  protected EmployeeManagerLocalHome employeeManagerLocalHome;  
  
  
  public DisplayTaskFormAction () throws NamingException {
    super();
    requestManagerLocalHome = 
      Utils.getRequestManagerHomeInterface();
    
    employeeManagerLocalHome = 
      Utils.getEmployeeManagerHomeInterface();    
  }

  
  public String execute(
     ActionMapping actionMapping,
     HttpServletRequest request, 
     HttpServletResponse response
  ) throws IOException, ServletException {
    try {
     TaskBean taskBean;

     int leadExecutorId = Utils.getUserId(request);
     
     int taskId = idVal(request.getParameter("task_id"));
     int requestId = idVal(request.getParameter("request_id"));     
     
     RequestManagerLocalObject requestManager = 
       requestManagerLocalHome.create();
     
     RequestBean requestBean = requestManager.selectRequest(requestId);
     
     if(requestBean.getStatusId() == 6) {
       Collection<TaskBean> tasksList = 
         requestManager.selectTasksByRequest(
           requestId
         );        
       
       requestManager.remove();
                            
       addErrorMessage(
          request,
          config.getProperty("ERROR.EDIT_DENIED")
       );
       
       request.setAttribute("tasksList", tasksList);
       request.setAttribute("requestBean", requestBean);
       requestManager.remove();   
       return actionMapping.getFailureTarget();
     }
          
     EmployeeManagerLocalObject employeeManager = 
       employeeManagerLocalHome.create();
     
     Collection<EmployeeBean> employeesList = 
       employeeManager.selectSubordinates(leadExecutorId);          
     
     
     Action action = Action.valueOf(actionMapping.getActionName());

     switch (action) {
        case addTaskForm:
           taskBean = new TaskBean();
           taskBean.setRequestId(requestId);
           taskBean.setPriority(3);
           
           Calendar calendar = Calendar.getInstance();
           taskBean.setStartDate(calendar.getTime());
           taskBean.setApproxEndDate(calendar.getTime());
           
           request.setAttribute("action", "processAddTaskForm");
           break;
           
        case editTaskForm:
           taskBean = requestManager.selectTask(taskId);
           request.setAttribute("action", "processEditTaskForm");
           break;
           
        default:
           throw new RuntimeException("Unsupported action " + action);
     }         
      
          
     Collection <RequestPriorityBean> priorityList = 
       requestManager.selectPriorityList();     
     
     request.setAttribute("priorityList", priorityList);
     request.setAttribute("employeesList", employeesList);
     request.setAttribute("taskBean", taskBean);
     
     requestManager.remove();
     employeeManager.remove();
     
     return actionMapping.getSuccessTarget();
    } catch (Exception e) {
      handleException(e, "execute");
      throw new InternalException("ErrorMessage.internal");
    }
  }
}